#include "command_test.h"
#include "usart.h"
// 比较
int test_compare(void) {
  Bool pre = true;
  // 测试 Byte 比较
  Byte byte_result = CompareByte(pre, 100, 100, CMP_EQ);
  ser3_send_log("Byte: 100 == 100 -> %d\n", byte_result); // 应输出 1
  delay_ms(200);
  // 测试 Word 比较
  Word word_result = CompareWord(pre, 1000, 1000, CMP_GE);
  ser3_send_log("Word: 1000 >= 1000 -> %d\n", word_result); // 应输出 1
  delay_ms(200);
  // 测试 DWord 比较
  DWord dword_result = CompareDWord(pre, 50000, 40000, CMP_GT);
  ser3_send_log("DWord: 50000 > 40000 -> %d\n", dword_result); // 应输出 1
  delay_ms(200);
  // 测试 LWord 比较
  LWord lword_result = CompareLWord(pre, 1000000ULL, 2000000ULL, CMP_LT);
  ser3_send_log("LWord: 1000000 < 2000000 -> %llu\n", lword_result); // 应输出 1
  delay_ms(200);
  // 测试 SInt 比较
  SInt sint_result = CompareSInt(pre, -50, -50, CMP_EQ);
  ser3_send_log("SInt: -50 == -50 -> %d\n", sint_result); // 应输出 1
  delay_ms(200);
  // 测试 USInt 比较
  USInt usint_result = CompareUSInt(pre, 200, 150, CMP_NE);
  ser3_send_log("USInt: 200 != 150 -> %d\n", usint_result); // 应输出 1
  delay_ms(200);
  // 测试 Int 比较
  Int int_result = CompareInt(pre, -10, 20, CMP_LT);
  ser3_send_log("Int: -10 < 20 -> %d\n", int_result); // 应输出 1
  delay_ms(200);
  // 测试 UInt 比较
  UInt uint_result = CompareUInt(pre, 3000, 2000, CMP_GT);
  ser3_send_log("UInt: 3000 > 2000 -> %d\n", uint_result); // 应输出 1
  delay_ms(200);
  // 测试 DInt 比较
  DInt dint_result = CompareDInt(pre, 100000, 200000, CMP_NE);
  ser3_send_log("DInt: 100000 != 200000 -> %d\n", dint_result); // 应输出 1
  delay_ms(200);
  // 测试 UDInt 比较
  UDInt udint_result = CompareUDInt(pre, 500000, 500000, CMP_LE);
  ser3_send_log("UDInt: 500000 <= 500000 -> %d\n", udint_result); // 应输出 1
  delay_ms(200);
  // 测试 LInt 比较
  LInt lint_result = CompareLInt(pre, -1000000LL, -2000000LL, CMP_GE);
  ser3_send_log("LInt: -1000000 >= -2000000 -> %lld\n",
                lint_result); // 应输出 1
  delay_ms(200);
  // 测试 ULInt 比较
  ULInt ulint_result = CompareULInt(pre, 1000000ULL, 1000000ULL, CMP_EQ);
  ser3_send_log("ULInt: 1000000 == 1000000 -> %llu\n",
                ulint_result); // 应输出 1
  delay_ms(200);
  // 测试 Real 比较
  Real real_result = CompareReal(pre, 3.14159f, 3.14159f, CMP_EQ);
  ser3_send_log("Real: 3.14159 == 3.14159 -> %f\n", real_result); // 应输出 1
  delay_ms(200);
  // 测试 LReal 比较
  LReal lreal_result =
      CompareLReal(pre, 3.141592653589793, 3.141592653589793, CMP_EQ);
  ser3_send_log("LReal: 3.141592653589793 == 3.141592653589793 -> %f\n",
                lreal_result); // 应输出 1
  delay_ms(200);
  // 范围比较
  //  测试 Byte
  ser3_send_log("Byte: 100 in [50, 150] -> %d\n",
                InRangeByte(pre, 100, 50, 150)); // 1
  delay_ms(200);
  ser3_send_log("Byte: 200 out of [50, 150] -> %d\n",
                OutOfRangeByte(pre, 200, 50, 150)); // 1
  delay_ms(200);

  // 测试 Word
  ser3_send_log("Word: 1000 in [500, 1500] -> %d\n",
                InRangeWord(pre, 1000, 500, 1500)); // 1
  delay_ms(200);
  ser3_send_log("Word: 2000 out of [500, 1500] -> %d\n",
                OutOfRangeWord(pre, 2000, 500, 1500)); // 1
  delay_ms(200);

  // 测试 DWord
  ser3_send_log("DWord: 50000 in [40000, 60000] -> %d\n",
                InRangeDWord(pre, 50000, 40000, 60000)); // 1
  delay_ms(200);
  ser3_send_log("DWord: 70000 out of [40000, 60000] -> %d\n",
                OutOfRangeDWord(pre, 70000, 40000, 60000)); // 1
  delay_ms(200);

  // 测试 LWord
  ser3_send_log("LWord: 1000000 in [500000, 1500000] -> %d\n",
                InRangeLWord(pre, 1000000ULL, 500000ULL, 1500000ULL)); // 1
  delay_ms(200);
  ser3_send_log("LWord: 2000000 out of [500000, 1500000] -> %d\n",
                OutOfRangeLWord(pre, 2000000ULL, 500000ULL, 1500000ULL)); // 1
  delay_ms(200);

  // 测试 SInt
  ser3_send_log("SInt: -50 in [-100, 0] -> %d\n",
                InRangeSInt(pre, -50, -100, 0)); // 1
  delay_ms(200);
  ser3_send_log("SInt: 50 out of [-100, 0] -> %d\n",
                OutOfRangeSInt(pre, 50, -100, 0)); // 1
  delay_ms(200);

  // 测试 USInt
  ser3_send_log("USInt: 100 in [50, 150] -> %d\n",
                InRangeUSInt(pre, 100, 50, 150)); // 1
  delay_ms(200);
  ser3_send_log("USInt: 200 out of [50, 150] -> %d\n",
                OutOfRangeUSInt(pre, 200, 50, 150)); // 1
  delay_ms(200);

  // 测试 Int
  ser3_send_log("Int: -10 in [-20, 0] -> %d\n",
                InRangeInt(pre, -10, -20, 0)); // 1
  delay_ms(200);
  ser3_send_log("Int: 10 out of [-20, 0] -> %d\n",
                OutOfRangeInt(pre, 10, -20, 0)); // 1
  delay_ms(200);

  // 测试 UInt
  ser3_send_log("UInt: 1000 in [500, 1500] -> %d\n",
                InRangeUInt(pre, 1000, 500, 1500)); // 1
  delay_ms(200);
  ser3_send_log("UInt: 2000 out of [500, 1500] -> %d\n",
                OutOfRangeUInt(pre, 2000, 500, 1500)); // 1
  delay_ms(200);

  // 测试 DInt
  ser3_send_log("DInt: 100000 in [50000, 150000] -> %d\n",
                InRangeDInt(pre, 100000, 50000, 150000)); // 1
  delay_ms(200);
  ser3_send_log("DInt: 200000 out of [50000, 150000] -> %d\n",
                OutOfRangeDInt(pre, 200000, 50000, 150000)); // 1
  delay_ms(200);
  // 测试 UDInt
  ser3_send_log("UDInt: 500000 in [400000, 600000] -> %d\n",
                InRangeUDInt(pre, 500000, 400000, 600000)); // 1
  delay_ms(200);
  ser3_send_log("UDInt: 700000 out of [400000, 600000] -> %d\n",
                OutOfRangeUDInt(pre, 700000, 400000, 600000)); // 1
  delay_ms(200);
  // 测试 LInt
  ser3_send_log("LInt: -1000000 in [-2000000, 0] -> %d\n",
                InRangeLInt(pre, -1000000LL, -2000000LL, 0LL)); // 1
  delay_ms(200);
  ser3_send_log("LInt: 1000000 out of [-2000000, 0] -> %d\n",
                OutOfRangeLInt(pre, 1000000LL, -2000000LL, 0LL)); // 1
  delay_ms(200);
  // 测试 ULInt
  ser3_send_log("ULInt: 1000000 in [500000, 1500000] -> %d\n",
                InRangeULInt(pre, 1000000ULL, 500000ULL, 1500000ULL)); // 1
  delay_ms(200);
  ser3_send_log("ULInt: 2000000 out of [500000, 1500000] -> %d\n",
                OutOfRangeULInt(pre, 2000000ULL, 500000ULL, 1500000ULL)); // 1
  delay_ms(200);

  // 测试 Real
  ser3_send_log("Real: 3.14159 in [3.0, 3.2] -> %d\n",
                InRangeReal(pre, 3.14159f, 3.0f, 3.2f)); // 1
  delay_ms(200);
  ser3_send_log("Real: 3.5 out of [3.0, 3.2] -> %d\n",
                OutOfRangeReal(pre, 3.5f, 3.0f, 3.2f)); // 1
  delay_ms(200);

  // 测试 LReal
  ser3_send_log("LReal: 3.141592653589793 in [3.0, 3.2] -> %d\n",
                InRangeLReal(pre, 3.141592653589793, 3.0, 3.2)); // 1
  delay_ms(200);
  ser3_send_log("LReal: 3.5 out of [3.0, 3.2] -> %d\n",
                OutOfRangeLReal(pre, 3.5, 3.0, 3.2)); // 1
  delay_ms(200);

  return 0;
}
// 数学指令
int test_add(void) {
  SInt sint_out;
  USInt usint_out;
  Int int_out;
  UInt uint_out;
  DInt dint_out;
  UDInt udint_out;
  Real real_out;
  LReal lreal_out;
  Bool res;

  // 测试 SInt
  ser3_send_log("test math command: add");
  delay_ms(200);

  res = AddSInt(1, &sint_out, 10, 20, 5, 0, 0, 0, 0, 0, 0, 0);
  ser3_send_log("SInt: 10 + 20 + 5 = %d, Result: %d\n", sint_out, res); // 35, 1
  delay_ms(200);

  res = AddSInt(1, &sint_out, 100, 100, 0, 0, 0, 0, 0, 0, 0, 0);
  ser3_send_log("SInt: Overflow (100 + 100) = %d, Result: %d\n", sint_out,
                res); // 0 (越界)
  delay_ms(200);

  // 测试 USInt
  res = AddUSInt(1, &usint_out, 50, 60, 10, 0, 0, 0, 0, 0, 0, 0);
  ser3_send_log("USInt: 50 + 60 + 10 = %d, Result: %d\n", usint_out,
                res); // 120, 1
  delay_ms(200);
  res = AddUSInt(1, &usint_out, 200, 100, 0, 0, 0, 0, 0, 0, 0, 0);
  ser3_send_log("USInt: Overflow (200 + 100) = %d, Result: %d\n", usint_out,
                res); // 0 (越界)
  delay_ms(200);

  // 测试 Int
  res = AddInt(1, &int_out, 1000, 2000, 0, 0, 0, 0, 0, 0, 0, 0);
  ser3_send_log("Int: 1000 + 2000 = %d, Result: %d\n", int_out, res); // 3000, 1
  delay_ms(200);

  res = AddInt(0, &int_out, 1000, 2000, 0, 0, 0, 0, 0, 0, 0, 0);
  ser3_send_log("Int: EN false = %d\n", res); // 0
  delay_ms(200);

  // 测试 UInt
  res = AddUInt(1, &uint_out, 5000, 6000, 1000, 0, 0, 0, 0, 0, 0, 0);
  ser3_send_log("UInt: 5000 + 6000 + 1000 = %d, Result: %d\n",

                uint_out, res); // 12000, 1
  delay_ms(200);

  // 测试 DInt
  res = AddDInt(1, &dint_out, 100000, 200000, 0, 0, 0, 0, 0, 0, 0, 0);
  ser3_send_log("DInt: 100000 + 200000 = %d, Result: %d\n",

                dint_out, res); // 300000, 1
  delay_ms(200);

  // 测试 UDInt
  res = AddUDInt(1, &udint_out, 500000, 600000, 0, 0, 0, 0, 0, 0, 0, 0);
  ser3_send_log("UDInt: 500000 + 600000 = %u, Result: %d\n", udint_out,
                res); // 1100000, 1
  delay_ms(200);

  // 测试 Real
  res = AddReal(1, &real_out, 3.14, 2.71, 1.0, 0, 0, 0, 0, 0, 0, 0, 0);
  ser3_send_log("Real: 3.14 + 2.71 + 1.0 = %f, Result: %d\n", real_out,
                res); // 6.85, 1
  delay_ms(200);

  res = AddReal(1, &real_out, 1e38, 1e38, 0, 0, 0, 0, 0, 0, 0, 0, 0);
  ser3_send_log("Real: Overflow (1e38 + 1e38) = %d\n",
                res); // 0 (无穷大)
  delay_ms(200);

  // 测试 LReal
  res = AddLReal(1, &lreal_out, 3.14159, 2.71828, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, );
  ser3_send_log("LReal: 3.14159 + 2.71828 = %lf, Result: %d\n", lreal_out,
                res); // 5.85987, 1
  delay_ms(200);

  return false;
}

int test_mul(void) {
  SInt sint_out;
  USInt usint_out;
  Int int_out;
  UInt uint_out;
  DInt dint_out;
  UDInt udint_out;
  Real real_out;
  LReal lreal_out;
  Bool res;
  ser3_send_log("test math command: mul");
  delay_ms(200);
  // 测试 SInt
  ser3_send_log("SInt: 2 * 3 * 5 = %d, Result: %d\n",
                MulSInt(1, &sint_out, 2, 3, 5, 1, 1, 1, 1, 1, 1, 1),
                sint_out); // 30, 1
  delay_ms(200);
  ser3_send_log("SInt: Overflow (50 * 3) = %d, Result: %d\n",
                MulSInt(1, &sint_out, 50, 3, 1, 1, 1, 1, 1, 1, 1, 1),
                sint_out); // 0 (越界)
  delay_ms(200);

  // 测试 USInt
  ser3_send_log("USInt: 4 * 5 * 2 = %d, Result: %d\n",
                MulUSInt(1, &usint_out, 4, 5, 2, 1, 1, 1, 1, 1, 1, 1),
                usint_out); // 40, 1
  delay_ms(200);
  ser3_send_log("USInt: Overflow (100 * 3) = %d, Result: %d\n",
                MulUSInt(1, &usint_out, 100, 3, 1, 1, 1, 1, 1, 1, 1, 1),
                usint_out); // 0 (越界)
  delay_ms(200);

  // 测试 Int
  ser3_send_log("Int: 10 * 20 = %d, Result: %d\n",
                MulInt(1, &int_out, 10, 20, 1, 1, 1, 1, 1, 1, 1, 1),
                int_out); // 200, 1
  delay_ms(200);
  ser3_send_log("Int: EN false = %d\n",
                MulInt(0, &int_out, 10, 20, 1, 1, 1, 1, 1, 1, 1, 1)); // 0

  // 测试 UInt
  ser3_send_log("UInt: 50 * 60 * 2 = %d, Result: %d\n",
                MulUInt(1, &uint_out, 50, 60, 2, 1, 1, 1, 1, 1, 1, 1),
                uint_out); // 6000, 1
  delay_ms(200);

  // 测试 DInt
  ser3_send_log("DInt: 100 * 200 = %d, Result: %d\n",
                MulDInt(1, &dint_out, 100, 200, 1, 1, 1, 1, 1, 1, 1, 1),
                dint_out); // 20000, 1
  delay_ms(200);

  // 测试 UDInt
  ser3_send_log("UDInt: 500 * 600 = %u, Result: %d\n",
                MulUDInt(1, &udint_out, 500, 600, 1, 1, 1, 1, 1, 1, 1, 1),
                udint_out); // 300000, 1
  delay_ms(200);

  // 测试 Real
  ser3_send_log("Real: 2.5 * 1.5 * 2.0 = %hhu, Result: %f\n",
                MulReal(1, &real_out, 2.5f, 1.5f, 2.0f, 1.0f, 1.0f, 1.0f, 1.0f,
                        1.0f, 1.0f, 1.0f),
                real_out); // 7.5, 1
  delay_ms(200);
  ser3_send_log("Real: Overflow (1e20 * 1e20) = %d\n",
                MulReal(1, &real_out, 1e20f, 1e20f, 1.0f, 1.0f, 1.0f, 1.0f,
                        1.0f, 1.0f, 1.0f, 1.0f)); // 0 (无穷大)
  delay_ms(200);

  // 测试 LReal
  ser3_send_log("LReal: 2.718 * 3.142 = %hhu, Result: %f\n",
                MulLReal(1, &lreal_out, 2.718, 3.142, 1.0, 1.0, 1.0, 1.0, 1.0,
                         1.0, 1.0, 1.0),
                lreal_out); // 8.539596, 1
  delay_ms(200);

  return false;
}

// 测试减法和除法函数
int test_div_sub(void) {
  SInt sint_out;
  USInt usint_out;
  Int int_out;
  UInt uint_out;
  DInt dint_out;
  UDInt udint_out;
  Real real_out;
  LReal lreal_out;

  // 测试 SInt
  ser3_send_log("SInt Sub: 10 - 3 = %d, Result: %d\n",
                SubSInt(1, &sint_out, 10, 3),
                sint_out); // 7, 1
  delay_ms(200);
  ser3_send_log("SInt Div: 10 / 2 = %d, Result: %d\n",
                DivSInt(1, &sint_out, 10, 2),
                sint_out); // 5, 1
  delay_ms(200);
  ser3_send_log("SInt Div by 0 = %d\n", DivSInt(1, &sint_out, 10, 0)); // 0
  delay_ms(200);

  // 测试 USInt
  ser3_send_log("USInt Sub: 20 - 5 = %d, Result: %d\n",
                SubUSInt(1, &usint_out, 20, 5),
                usint_out); // 15, 1
  delay_ms(200);
  ser3_send_log("USInt Sub Overflow: 5 - 10 = %d, Result: %d\n",
                SubUSInt(1, &usint_out, 5, 10), usint_out); // 0 (负数)
  delay_ms(200);
  ser3_send_log("USInt Div: 20 / 4 = %d, Result: %d\n",
                DivUSInt(1, &usint_out, 20, 4),
                usint_out); // 5, 1
  delay_ms(200);

  // 测试 Int
  ser3_send_log("Int Sub: 1000 - 200 = %d, Result: %d\n",
                SubInt(1, &int_out, 1000, 200), int_out); // 800, 1
  delay_ms(200);
  ser3_send_log("Int Div: 1000 / 5 = %d, Result: %d\n",
                DivInt(1, &int_out, 1000, 5),
                int_out); // 200, 1
  delay_ms(200);
  ser3_send_log("Int EN false = %d\n", SubInt(0, &int_out, 1000, 200)); // 0
  delay_ms(200);

  // 测试 UInt
  ser3_send_log("UInt Sub: 5000 - 1000 = %d, Result: %d\n",
                SubUInt(1, &uint_out, 5000, 1000), uint_out); // 4000, 1
  delay_ms(200);
  ser3_send_log("UInt Div: 5000 / 2 = %d, Result: %d\n",
                DivUInt(1, &uint_out, 5000, 2), uint_out); // 2500, 1
  delay_ms(200);

  // 测试 DInt
  ser3_send_log("DInt Sub: 100000 - 50000 = %d, Result: %d\n",
                SubDInt(1, &dint_out, 100000, 50000), dint_out); // 50000, 1
  delay_ms(200);
  ser3_send_log("DInt Div: 100000 / 10 = %d, Result: %d\n",
                DivDInt(1, &dint_out, 100000, 10), dint_out); // 10000, 1
  delay_ms(200);

  // 测试 UDInt
  ser3_send_log("UDInt Sub: 1000000 - 200000 = %u, Result: %d\n",
                SubUDInt(1, &udint_out, 1000000, 200000),
                udint_out); // 800000, 1
  delay_ms(200);
  ser3_send_log("UDInt Div: 1000000 / 5 = %u, Result: %d\n",
                DivUDInt(1, &udint_out, 1000000, 5), udint_out); // 200000, 1
  delay_ms(200);

  // 测试 Real
  ser3_send_log("Real Sub: 5.5 - 2.2 = %hhu, Result: %f\n",
                SubReal(1, &real_out, 5.5f, 2.2f), real_out); // 3.3, 1
  delay_ms(200);
  ser3_send_log("Real Div: 5.5 / 2.0 = %hhu, Result: %f\n",
                DivReal(1, &real_out, 5.5f, 2.0f), real_out); // 2.75, 1
  delay_ms(200);
  ser3_send_log("Real Div by 0 = %d\n", DivReal(1, &real_out, 5.5f, 0.0f)); // 0
  delay_ms(200);

  // 测试 LReal
  ser3_send_log("LReal Sub: 3.14159 - 2.71828 = %hhu, Result: %f\n",
                SubLReal(1, &lreal_out, 3.14159, 2.71828),
                lreal_out); // 0.42331, 1
  delay_ms(200);
  ser3_send_log("LReal Div: 3.14159 / 2.0 = %hhu, Result: %f\n",
                DivLReal(1, &lreal_out, 3.14159, 2.0),
                lreal_out); // 1.570795, 1
  delay_ms(200);

  return 0;
}

// 测试取余函数
int test_mod(void) {
  SInt sint_out;
  USInt usint_out;
  Int int_out;
  UInt uint_out;
  DInt dint_out;
  UDInt udint_out;

  // 测试 SInt
  ser3_send_log("SInt Mod: 10 %% 3 = %d, Result: %d\n",
                ModSInt(1, &sint_out, 10, 3),
                sint_out); // 1, 1
  delay_ms(200);
  ser3_send_log("SInt Mod by 0 = %d\n", ModSInt(1, &sint_out, 10, 0)); // 0
  delay_ms(200);
  // 测试 USInt
  ser3_send_log("USInt Mod: 20 %% 6 = %d, Result: %d\n",
                ModUSInt(1, &usint_out, 20, 6), usint_out); // 2, 1
  delay_ms(200);
  ser3_send_log("USInt Mod by 0 = %d\n", ModUSInt(1, &usint_out, 20, 0)); // 0
  delay_ms(200);
  // 测试 Int
  ser3_send_log("Int Mod: 1000 %% 7 = %d, Result: %d\n",
                ModInt(1, &int_out, 1000, 7),
                int_out); // 6, 1
  delay_ms(200);
  ser3_send_log("Int EN false = %d\n", ModInt(0, &int_out, 1000, 7)); // 0
  delay_ms(200);

  // 测试 UInt
  ser3_send_log("UInt Mod: 5000 %% 9 = %d, Result: %d\n",
                ModUInt(1, &uint_out, 5000, 9), uint_out); // 5, 1
  delay_ms(200);
  // 测试 DInt
  ser3_send_log("DInt Mod: 100000 %% 11 = %d, Result: %d\n",
                ModDInt(1, &dint_out, 100000, 11), dint_out); // 1, 1
  delay_ms(200);
  // 测试 UDInt
  ser3_send_log("UDInt Mod: 1000000 %% 13 = %u, Result: %d\n",
                ModUDInt(1, &udint_out, 1000000, 13), udint_out); // 9, 1
  delay_ms(200);
  return 0;
}

// 测试递增和递减函数
int test_inc_dec(void) {
  SInt sint_val = 126;
  USInt usint_val = 254;
  Int int_val = 32766;
  UInt uint_val = 65534;
  DInt dint_val = 2147483646;
  UDInt udint_val = 4294967294;

  // 测试 SInt
  ser3_send_log("SInt Inc: 126 -> %d, Result: %d\n", IncSInt(1, &sint_val),
                sint_val); // 127, 1
  delay_ms(200);
  ser3_send_log("SInt Inc Overflow: 127 -> %d, Result: %d\n",
                IncSInt(1, &sint_val),
                sint_val); // -128, 1
  delay_ms(200);
  ser3_send_log("SInt Dec: -128 -> %d, Result: %d\n", DecSInt(1, &sint_val),
                sint_val); // 127, 1
  delay_ms(200);
  ser3_send_log("SInt EN false = %d\n", IncSInt(0, &sint_val)); // 0
  delay_ms(200);

  // 测试 USInt
  ser3_send_log("USInt Inc: 254 -> %d, Result: %d\n", IncUSInt(1, &usint_val),
                usint_val); // 255, 1
  delay_ms(200);
  ser3_send_log("USInt Inc Overflow: 255 -> %d, Result: %d\n",
                IncUSInt(1, &usint_val),
                usint_val); // 0, 1
  delay_ms(200);
  ser3_send_log("USInt Dec: 0 -> %d, Result: %d\n", DecUSInt(1, &usint_val),
                usint_val); // 255, 1
  delay_ms(200);

  // 测试 Int
  ser3_send_log("Int Inc: 32766 -> %d, Result: %d\n", IncInt(1, &int_val),
                int_val); // 32767, 1
  delay_ms(200);
  ser3_send_log("Int Inc Overflow: 32767 -> %d, Result: %d\n",
                IncInt(1, &int_val),
                int_val); // -32768, 1
  delay_ms(200);

  // 测试 UInt
  ser3_send_log("UInt Inc: 65534 -> %d, Result: %d\n", IncUInt(1, &uint_val),
                uint_val); // 65535, 1
  delay_ms(200);
  ser3_send_log("UInt Inc Overflow: 65535 -> %d, Result: %d\n",
                IncUInt(1, &uint_val),
                uint_val); // 0, 1
  delay_ms(200);

  // 测试 DInt
  ser3_send_log("DInt Inc: 2147483646 -> %d, Result: %d\n",
                IncDInt(1, &dint_val),
                dint_val); // 2147483647, 1
  delay_ms(200);
  ser3_send_log("DInt Inc Overflow: 2147483647 -> %d, Result: %d\n",
                IncDInt(1, &dint_val), dint_val); // -2147483648, 1
  delay_ms(200);
  // 测试 UDInt
  ser3_send_log("UDInt Inc: 4294967294 -> %u, Result: %d\n",
                IncUDInt(1, &udint_val),
                udint_val); // 4294967295, 1
  delay_ms(200);
  ser3_send_log("UDInt Inc Overflow: 4294967295 -> %u, Result: %d\n",
                IncUDInt(1, &udint_val), udint_val); // 0, 1
  delay_ms(200);

  return 0;
}
// 位指令
int testPContact(void) {
  Bool bit = false; // Initialize edge storage bit
  Bool operand;     // Operand signal state
  Bool in;          // Power flow input
  Bool result;      // Detection result

  // Test sequences for operand and power flow
  Bool operand_sequence[] = {1, 0, 0, 1, 0, 0};
  Bool in_sequence[] = {1, 1, 0, 1, 1, 0};
  int sequence_length = sizeof(operand_sequence) / sizeof(operand_sequence[0]);

  // printf("Testing falling edge detection with power flow:\r\n");
  ser3_send_log("Testing falling edge detection with power flow:\r\n");
  delay_ms(200);
  for (int i = 0; i < sequence_length; i++) {
    operand = operand_sequence[i];
    in = in_sequence[i];
    result = PContact(in, operand, &bit);
    // printf("Cycle %d: Power Flow = %d, Operand = %d, Edge Bit = %d, Result =
    // "
    //        "%d\r\n",
    //        i + 1, in, operand, bit, result);
    ser3_send_log(
        "Cycle %d: Power Flow = %d, Operand = %d, Edge Bit = %d, Result = "
        "%d\r\n",
        i + 1, in, operand, bit, result);
    delay_ms(200);
  }

  return 0;
}

int testNContact(void) {
  Bool bit = false; // 初始化边沿存储位
  Bool operand;     // 操作数信号状态
  Bool in;          // 能流输入
  Bool result;      // 检测结果

  // 操作数和能流测试序列
  Bool operand_sequence[] = {1, 0, 0, 1, 0, 0};
  Bool in_sequence[] = {1, 1, 0, 1, 1, 0};
  int sequence_length = sizeof(operand_sequence) / sizeof(operand_sequence[0]);

  // printf("测试带能流的下降沿检测：\n");
  ser3_send_log(
      "Test the detection of the falling edge with the energy flow: \n");
  delay_ms(200);
  for (int i = 0; i < sequence_length; i++) {
    operand = operand_sequence[i];
    in = in_sequence[i];
    result = NContact(in, operand, &bit);
    ser3_send_log(
        "Cycle %d: Power Flow = %d, Operand = %d, Edge Bit = %d, Result = %d\n",
        i + 1, in, operand, bit, result);
    delay_ms(200);
  }
  return 0;
}

int test_PCoil(void) {
  Bool operand = false; // 初始化操作数
  Bool bit = false;     // 初始化边沿存储位
  Bool in;              // RLO 输入

  // RLO 测试序列
  Bool power_flow_sequence[] = {0, 1, 1, 0, 1, 0};
  int sequence_length =
      sizeof(power_flow_sequence) / sizeof(power_flow_sequence[0]);

  ser3_send_log("Instruction for setting the operand on the rising edge of the "
                "signal in the test：\n");
  delay_ms(200);
  for (int i = 0; i < sequence_length; i++) {
    in = power_flow_sequence[i];
    PCoil(in, &operand, &bit);
    ser3_send_log("Cycle %d: Power Flow = %d, Operand = %d, Edge Bit = %d\n",
                  i + 1, in, operand, bit);
    delay_ms(200);
  }

  return 0;
}

int test_NCoil(void) {
  Bool operand = false;  // 初始化操作数
  Bool edge_bit = false; // 初始化边沿存储位
  Bool power_flow;       // RLO 输入

  // RLO 测试序列
  Bool power_flow_sequence[] = {1, 0, 0, 1, 0, 1};
  int sequence_length =
      sizeof(power_flow_sequence) / sizeof(power_flow_sequence[0]);

  ser3_send_log("Instruction for testing the operation of setting the operand "
                "on the falling edge of the signal.：\n");
  delay_ms(200);
  for (int i = 0; i < sequence_length; i++) {
    power_flow = power_flow_sequence[i];
    NCoil(power_flow, &operand, &edge_bit);
    ser3_send_log("Cycle %d: Power Flow = %d, Operand = %d, Edge Bit = %d\n",
                  i + 1, power_flow, operand, edge_bit);
    delay_ms(200);
  }

  return 0;
}
